package com.cennavi.mineplay.mineplay_data.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionInfo;
import com.cennavi.mineplay.mineplay_data.model.DBTask;
import com.cennavi.mineplay.mineplay_data.model.DBTaskAssember;
import com.cennavi.mineplay.mineplay_data.model.DbNameInfo;
import com.cennavi.mineplay.mineplay_data.model.HttpConnectionInfo;
import com.cennavi.mineplay.mineplay_data.model.HttpTask;
import com.cennavi.mineplay.mineplay_data.model.HttpTaskAssember;
import com.cennavi.mineplay.mineplay_data.model.TableNameInfo;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.MysqlConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.OracleConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.PostgresConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.DBConnectionImp.ServersqlConnectionImpl;
import com.cennavi.mineplay.mineplay_data.model.db.inter.DBConnection;
import com.cennavi.mineplay.mineplay_data.model.transform.inter.Transform;


@RestController
@RequestMapping(value = "/custom")
public class Customer {
	
	@Autowired
	protected JdbcTemplate jdbc;
	
	@Value("${spring.datasource.url}")
	private String destDBJdbc;
	
	@Value("${spring.datasource.username}")
	private String username;
	
	@Value("${spring.datasource.password}")
	private String password;
	
	@Value("${spring.datasource.driver-class-name}")
	private String dbDriverClass;
	
	@Value("${table.name}")
	private String destTableNames;

	@RequestMapping("/pluge_into")
	public Object pluge_into(String data) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		System.out.println(data);
		
		JSONObject json = JSONObject.parseObject(data);
		
		String datasourcetype = json.getString("datasourcetype");
		
		if ("http".equals(datasourcetype)){
			
//			HttpConnectionInfo httpinfo = new HttpConnectionInfo();
//			
//			httpinfo.setCompany_id(json.getIntValue("company_id"));
//			
//			httpinfo.setFrequency(json.getIntValue("frequency"));
//			
//			HttpTask task = new HttpTask(httpinfo,null, destDBJdbc, dbDriverClass, username, password,destTableNames);
			
			HttpTask task = new HttpTaskAssember(json, 
					destDBJdbc,
					dbDriverClass,
					username, 
					password, 
					destTableNames).assember();
			
			task.start();
		}else if ("db".equals(datasourcetype)){
			
//			DBConnectionInfo dbinfo = new DBConnectionInfo();
//			
//			dbinfo.setCompany_id(json.getIntValue("company_id"));
//			
//			dbinfo.setDriverClass("");
//			
//			dbinfo.setDbType(json.getString("dbtype"));
//			
//			dbinfo.setJdbc(json.getString("jdbcurl"));
//			
//			dbinfo.setFrequency(json.getIntValue("frequency"));
//			
//			dbinfo.setPassword(json.getString("password"));
//			
//			dbinfo.setSql(json.getString("sql"));
//			
//			dbinfo.setTimeName(json.getString("timeName"));
//			
//			dbinfo.setUpdateStyle(json.getIntValue("updateStyle"));
//			
//			dbinfo.setUsername(json.getString("username"));
//			
//			DbNameInfo dbnameinfo = new DbNameInfo();
//			
//			dbnameinfo.setDbname(json.getString("dbname"));
//			
//			dbnameinfo.setDbname_prefix(json.getString("dbname_prefix"));
//			
//			dbnameinfo.setIs_day(json.getIntValue("db_is_day"));
//			
//			dbnameinfo.setIs_dynamic(json.getIntValue("db_is_dynamic"));
//			
//			dbnameinfo.setIs_month(json.getIntValue("db_is_month"));
//			
//			dbnameinfo.setIs_month_split(json.getIntValue("db_is_month_split"));
//			
//			//前端没有加入
//			dbnameinfo.setMonth_prefix(json.getString("db_month_prefix") == null ? "P" : json.getString("db_month_prefix"));
//			
//			dbnameinfo.setNum_per_month(json.getIntValue("num_per_month"));
//			
//			TableNameInfo tablenameinfo = new TableNameInfo();
//			
//			tablenameinfo.setIs_one_table(json.getIntValue("is_one_table"));
//			
//			tablenameinfo.setTableName(json.getString("tableName"));
//			
//			tablenameinfo.setTableName_prefix(json.getString("tableName_prefix"));
//			
//			DBConnection dbconn = null;
//			
//			if ("oracle".equals(dbinfo.getDbType())){
//				dbconn = new OracleConnectionImpl(dbnameinfo, dbinfo);
//			}else if ("sqlserver".equals(dbinfo.getDbType())){
//				dbconn = new ServersqlConnectionImpl(dbnameinfo, dbinfo);
//			}else if ("mysql".equals(dbinfo.getDbType())){
//				dbconn = new MysqlConnectionImpl(dbnameinfo, dbinfo);
//			}else if ("postgresql".equals(dbinfo.getDbType())){
//				dbconn = new PostgresConnectionImpl(dbnameinfo, dbinfo);
//			}
//			
//			List<Transform> trans = null;
//			
//			DBTask task = new DBTask(dbconn, dbinfo, dbnameinfo, tablenameinfo, trans, destDBJdbc, dbDriverClass, username, password,destTableNames);
//			
			
			DBTask task = new DBTaskAssember(json, 
					destDBJdbc,
					dbDriverClass,
					username, 
					password, 
					destTableNames).assember();
			
			task.start();
		}else{
			result.put("error_code", -1);
			result.put("error_msg", "data source type "+datasourcetype+" is unlegal!");
			
			return result;
		}
		
		String sql = "insert into task_param(param) values (?)";
		
		jdbc.update(sql, data);
		
		return result;
	}
	
	
	@RequestMapping("/task_list")
	public Object task_list() {
		
		
		return null;
	}
	
	@RequestMapping("/add_company")
	public Object add_company(String name,String describe) {
		
		String sql = "insert into company(name,describe) values (?,?)";
		
		jdbc.update(sql, name,describe);
		
		return "{}";
	}
	
	
	
	@RequestMapping("/company_list")
	public Object company_list() {
		Map<String,Object> map = new HashMap<String,Object>();
		
		String sql = "select id,name,describe from company";
		
		List<Map<String,Object>> list = jdbc.queryForList(sql);
		
		map.put("data", list);
		
		return map;
	}
	
	
}
